package cn.dapeng.base;

import cn.dapeng.util.SleepingUtil;

import java.util.concurrent.TimeUnit;


/**
 * 使用volatile修饰暂停开关，实现优雅关闭, 不能做到精准关闭，比如循环5次就关闭
 */
public class ThreadStop_T03 {

    private volatile static boolean running = true;

    public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            while (running) {
                System.out.println("go on");
                SleepingUtil.sleep(1, TimeUnit.SECONDS);
            }
        });

        thread.start();

        SleepingUtil.sleep(5, TimeUnit.SECONDS);
        running = false;
    }
}
